//
//  LHVisitorView.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/11.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit
import SnapKit

class LHVisitorView: UIView {
    
    //设置回调闭包
    var callBackClosure: (()->())?

    //旋转图片
    private lazy var cycleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩
    private lazy var coverImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //文字
    private lazy var messageLabel: UILabel = {
        let messageLabel = UILabel()
        messageLabel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        messageLabel.font = UIFont.systemFontOfSize(14)
        messageLabel.textAlignment = .Center
        return messageLabel
        
    }()
    
    //icon
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        return btn
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        return btn
    }()
    
    private func setupUI() {
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(coverImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //自动布局
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        coverImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.size.equalTo(CGSizeMake(224, 40))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVistorInfo(title: String? , imageName: String?) {
        if let t = title, img = imageName {
            //进入的不是首页
            messageLabel.text = t
            iconImageView.image = UIImage(named: img)
            cycleImageView.hidden = true
        }else{
            //进入的是首页
            cycleImageView.hidden = false
            startAnimation()
        }
        
    }
    
    @objc private func loginAction() {
        callBackClosure?()
    }
    
    @objc private func register() {
        callBackClosure?()
    }
    
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }

}
